#include "stdafx.h"
#include "wf_drawlinepolygon.h"

#include "pf_creation.h"
#include "pf_snapper.h"



WF_DrawLinePolygon::WF_DrawLinePolygon(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Draw Polygons", container, graphicView) {

    center = PF_Vector(false);
    corner = PF_Vector(false);

    number = 3;
}

QAction* WF_DrawLinePolygon::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// Polygon
    QAction* action = new QAction(tr("Pol&ygon (Cen,Cor)"),  NULL);
		action->setIcon(QIcon(":/extui/linespolygon.png"));
    //action->zetStatusTip(tr("Draw polygon with center and corner"));
    return action;
}


void WF_DrawLinePolygon::trigger() {
    PF_PreviewActionInterface::trigger();

    deletePreview();

    PF_Creation creation(container, graphicView);
    bool ok = creation.createPolygon(center, corner, number);

    if (!ok) {
        PF_DEBUG->print("PF_ActionDrawLinePolygon::trigger:"
                        " No polygon added\n");
    }
}



void WF_DrawLinePolygon::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionDrawLinePolygon::mouseMoveEvent begin");

    PF_Vector mouse = snapPoint(e);

    switch (getStatus()) {
    case SetCenter:
        break;

    case SetCorner:
        if (center.valid) {
            corner = mouse;
            deletePreview();

            PF_Creation creation(preview, NULL, false);
            creation.createPolygon(center, corner, number);

            drawPreview();
        }
        break;

    default:
        break;
    }
}



void WF_DrawLinePolygon::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_DrawLinePolygon::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector mouse = e->getCoordinate();

    switch (getStatus()) {
    case SetCenter:
        center = mouse;
        setStatus(SetCorner);
        graphicView->moveRelativeZero(mouse);
        break;

    case SetCorner:
        corner = mouse;
        trigger();
        break;

    default:
        break;
    }
}



void WF_DrawLinePolygon::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetCenter:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify center"),
                                                "");
            break;

        case SetCorner:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Specify a corner"), "");
            break;

        case SetNumber:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Enter number:"), "");
            break;

        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_DrawLinePolygon::showOptions() {
    PF_ActionInterface::showOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, true);
    }
}



void WF_DrawLinePolygon::hideOptions() {
    PF_ActionInterface::hideOptions();

    if (PF_DIALOGFACTORY!=NULL) {
        PF_DIALOGFACTORY->requestOptions(this, false);
    }
}



void WF_DrawLinePolygon::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        if (PF_DIALOGFACTORY!=NULL) {
            PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                             + getAvailableCommands().join(", "));
        }
        return;
    }

    switch (getStatus()) {
    case SetCenter:
    case SetCorner:
        if (checkCommand("number", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetNumber);
        }
        break;

    case SetNumber: {
            bool ok;
            int n = c.toInt(&ok);
            if (ok==true) {
                if (n>0 && n<10000) {
                    number = n;
                } else {
                    if (PF_DIALOGFACTORY!=NULL) {
                        PF_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
                                                            "Try 1..9999"));
                    }
                }
            } else {
                if (PF_DIALOGFACTORY!=NULL) {
                    PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
                }
            }
            if (PF_DIALOGFACTORY!=NULL) {
                PF_DIALOGFACTORY->requestOptions(this, true, true);
            }
            setStatus(lastStatus);
        }
        break;

    default:
        break;
    }
}



PF_StringList WF_DrawLinePolygon::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case SetCenter:
    case SetCorner:
        cmd += command("number");
        break;
    default:
        break;
    }

    return cmd;
}



void WF_DrawLinePolygon::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_DrawLinePolygon::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarLines);
        }
    }
}


// EOF
